<template>
<img src="./images/banner.jpg" style="position:absoult;top:0;left:0;width:100%;height:100%">
</template>
<style  lang='scss'>
.common {
  width: 100%;
  position: absolute;
  top: 20%;
  left: 0
}

.common-login {
  background: #ffffff;
  width: 490px;
  margin: 15px auto 0 auto;
}

.login-header {
  background: #dacdcd;
  padding-left: 25px;
  line-height: 43px;
  font-size: 18px;
  color: #2E6296;
  border-bottom: 1px solid #C6D8EF;
}

.login-box {
  padding: 22px 0 25px 90px;
  overflow: hidden;
}

.login-item {
  position: relative;
  width: 100%;
  height: 47px;

  .box-yzm {
    cursor: pointer;
    position: absolute;
    left: 179px;
    top: -3px;
  }
  span {
    display: block;
    position: absolute;
    top: 1px;
    left: 1px;
    height: 27px;
    width: 80px;
    line-height: 27px;
    text-align: center;
    background: #EFF3F9;
    color: #5D7289;
    font-size: 14px;
    border-right: 1px solid #C1C1C1;
  }
  .box-ipt {
    border: 1px solid #C1C1C1;
    height: 27px;
    line-height: 27px;
    width: 217px;
    font-size: 14px;
    font-family: Verdana, Arial;
    font-weight: bold;
    color: #333333;
    padding-left: 90px;
  }
  .box-Code {
    width: 80px;
  }
  .login-btn {
    background: #3F9FE7;
    padding: 9px 0;
    width: 310px;
    border: none;
    font-size: 16px;
    color: #FFFFFF;
    font-weight: bold;
    text-align: center;
  }
}
</style>

<script>
export default {
  data() {
    return {
      userName: '',
      password: '',
      code: '',
      verifyVal: ''
    }
  },
  async mounted() {
    // 自动登录
    const isLogin = this.$store.getters.getIsLogin
    const href = window.location.href
    const queryStr = href.slice(href.lastIndexOf('?') + 1)
    const queryArr = queryStr.split('&')
    let username , password
    queryArr.forEach(function(item){
      if(item.includes('username')){
        username = /username=([\u4E00-\u9FA5 A-Za-z]+)/gi.exec(decodeURIComponent(item))
        username = username && username.length > 0 ? username[1] : ''
      }else if(item.includes('password')){
        password = /password=(.+)/gi.exec(decodeURIComponent(item))
        password = password && password.length > 0 ? password[1] : ''
      }
    })
    if (!isLogin && username && password) {
      this.userName = username
      this.password = password
      await this.IPlogin()
      this.login()
    }
  },
  methods: {
    login() {
      if (!this.verify()) {
        this.$toast({
          message: this.verifyVal
        })
        return false
      }
      this.$get(this, {
        api: 'user/login.do',
         data: {
          username: this.userName,
          password: this.password,
          version:'2'  // 电话系统2
        }
      }).then(({ data }) => {
        if (data.state === 0) {
          console.log('登录成功')
          const tmpUser = Object.assign({},data.content.user)
          data.content.user.sessionId = data.content.sessionId
          // 重新设置token
          let token = data.content.token
          window.Vue.prototype.$get = window.initAjax('get', token)
          window.Vue.prototype.$post = window.initAjax('post', token)
          //
          // 现在的机制就没有必要要sessionId
          // document.cookie = 'userInfo=' + JSON.stringify(data.content.user)
          console.log('isLogin:', data.content.user)
          // console.log('globalIsLogin:', isLogin)
          // 触发聊天室
          isLogin.kfId = data.content.user.id
          isLogin.token = token
          isLogin.value = 1
          data.content.user.usertel = tmpUser.extenNumberNew
          this.$store.dispatch('updateLogin', data.content.user)
          this.$router.push('/')
          
        } else {
          this.$toast({
            message: data.message
          })
        }
      }).catch(e=>{
        console.log('login:', e)
      })
    },
    verify() {
      if (this.userName.length === 0) {
        this.verifyVal = '请输入用户名'
        return false
      }
      if (this.password.length === 0) {
        this.verifyVal = '请输入密码'
        return false
      }
      return true
    },
    async IPlogin(){
      const api = 'userWebsocket.do'
      const baseUrl = window.ipurl
      try{
        const { data } = await this.$get(this,{
          api,
          data:{
            username: this.userName,
            password: this.password,
            version:'2'  // 电话系统2
          }
        },baseUrl)
        if(data.state === 0){
          websocket.ipSessionId = data.content.sessionId
          return
        }
        console.warn(`系统接口${api}返回出错：${data.message}`)
      }catch(e){
        console.warn(`系统接口${api}执行出错信息：${e.message}`)
      }
    }
  }
}
</script>
<style>

</style>


